package com.xunqi.gulimall.product.exception;

import com.xunqi.common.exception.BizCodeEnum;
import com.xunqi.common.utils.R;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 集中处理所有异常
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-05-27 17:14
 **/

@Slf4j
@RestControllerAdvice(basePackages = "com.xunqi.gulimall.product.app")
public class GulimallExceptionControllerAdvice {

    /**
     * 参数非法（效验参数）异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R handleValidException(MethodArgumentNotValidException e) {
        log.error("数据效验出现问题{},异常类型{}", e.getMessage(), e.getClass());
        BindingResult result = e.getBindingResult();
        Map<String, String> errMap = new HashMap<>();
        result.getFieldErrors().forEach((fieldError) -> {
            errMap.put(fieldError.getField(), fieldError.getDefaultMessage());
        });

        // 显示一个异常
        // List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        // log.error("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
        // return R.error(fieldErrors.get(0).getDefaultMessage());

        return R.error(BizCodeEnum.VAILD_EXCEPTION.getCode(),
                        BizCodeEnum.VAILD_EXCEPTION.getMessage()).put("data", errMap);
    }


    @ExceptionHandler(value = Throwable.class)
    public R handleException(Throwable throwable) {
        log.error("错误异常{}", throwable);
        return R.error(BizCodeEnum.UNKNOW_EXCEPTION.getCode(), BizCodeEnum.UNKNOW_EXCEPTION.getMessage());
    }

}
